package com.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.entidades.Cliente;

@Stateless
public class ClienteDAO extends GenericDAO<Cliente> {

	@PersistenceContext(unitName = "Projeto2POD")
	private EntityManager em;
	
	public ClienteDAO() {
		super(Cliente.class);
	}
	
//	public ClienteDAO(Class<Cliente> entityClass) {
//		super(entityClass);
//	}

	public Cliente findByLogin(String login){		
		try{
			Query q = em.createQuery("select c from Cliente c where c.login='"+login+"'");
			Cliente c =(Cliente)q.getSingleResult();
			return c;
		} catch ( NoResultException e) {
			return null;
		}
	}

	public Cliente findByCPF(String CPF){
		try{
			Query q = em.createQuery("select c from Cliente c where c.CPF LIKE '%" + CPF + "%'");
			return (Cliente)q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} catch (NonUniqueResultException e) {
			return null;
		}
	}

	public Cliente findbyNome(String nome) {
		try{
			Query q = em.createQuery("select c from Cliente c where c.nome LIKE '%" + nome + "%'");
			return (Cliente)q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
}
